﻿using System;
using System.ComponentModel;
using System.Web.UI;

namespace HeroldIT.Dnn.Modules.WebControls
{
    /// <summary>
    /// A label control with selectable tag type.
    /// </summary>
    public class Label : System.Web.UI.WebControls.Label
    {
        #region properties

        /// <summary>
        /// Gets or sets whether to render the <see cref="Label"/> if it has no content.
        /// </summary>
        [DefaultValue(EmptyRenderMode.None)]
        [Localizable(false)]
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public EmptyRenderMode EmptyRenderMode { get; set; }

        /// <summary>
        /// Gets or sets whether to encode the Text when rendering it to the browser.
        /// </summary>
        [DefaultValue(false)]
        [Localizable(false)]
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public bool EncodeText { get; set; }

        /// <summary>
        /// Holds the tag to be rendered.
        /// </summary>
        private HtmlTextWriterTag tagKey = HtmlTextWriterTag.Unknown;

        /// <summary>
        /// Gets or sets the tag to be rendered.
        /// </summary>
        [DefaultValue(HtmlTextWriterTag.Unknown)]
        [Localizable(false)]
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public HtmlTextWriterTag Tag
        {
            get { return this.tagKey; }
            set { this.tagKey = value; }
        }

        /// <summary>
        /// Gets the HTML tag that is used to render the <see cref="Label" /> control.
        /// </summary>
        /// <returns>The <see cref="T:System.Web.UI.HtmlTextWriterTag" /> value used to render the <see cref="Label" />.</returns>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return String.IsNullOrEmpty(this.AssociatedControlID)
                           ? (this.tagKey == HtmlTextWriterTag.Unknown ? base.TagKey : this.tagKey)
                           : base.TagKey;
            }
        }

        #endregion

        #region methods

        /// <summary>
        /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter" /> object and stores tracing information about the control if tracing is enabled.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter" /> object that receives the control content.</param>
        public override void RenderControl(HtmlTextWriter writer)
        {
            if (this.EmptyRenderMode == EmptyRenderMode.None && this.Controls.Count <= 0 && String.IsNullOrEmpty(this.Text))
            {
                return;
            }
            base.RenderControl(writer);
        }

        /// <summary>
        /// Renders the contents of the <see cref="Label" /> into the specified writer.
        /// </summary>
        /// <param name="writer">The output stream that renders HTML content to the client.</param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.Controls.Count <= 0)
            {
                if (String.IsNullOrEmpty(this.Text) && this.EmptyRenderMode == EmptyRenderMode.NonBreakingSpace)
                {
                    writer.Write("&nbsp;");
                }
                else if (this.EncodeText)
                {
                    writer.WriteEncodedText(this.Text);
                }
                else
                {
                    base.RenderContents(writer);
                }
            }
            else
            {
                base.RenderContents(writer);
            }
        }

        #endregion
    }
}